package org.testis.communicationmodule.services;

import org.testis.data.TestStepException;
import org.testis.data.TestStepResultType;
import org.testis.services.AtofModuleAction;
import org.testis.services.AtofModuleActionFactory;
import org.testis.testdriver.AtofEnumaration.AtofCommunicationModuleAction;


/**
 * A factory class to created communication module action objects
 * @author nurhat
 */
public class CommunicationModuleActionFactory extends AtofModuleActionFactory
{

    /**
     * Creates an action object according to given action type
     * @param actionType
     * @return
     */
    @Override
    public AtofModuleAction getActionObject(String actionType)
    {
        AtofCommunicationModuleAction actionEnumType;
        AtofModuleAction moduleAction = null;
        try {
            actionEnumType = AtofCommunicationModuleAction.valueOf(actionType);
        } catch (IllegalArgumentException ex) {

            throw new TestStepException(TestStepResultType.MISTAKE,
                                        "Action type given " + actionType +
                                        " not found for Communication module ..." +
                                        ex.getMessage());
        }

        Class<?> cls;
        try {
            cls =
                Class.forName("org.testis.communicationmodule.services." +
                              actionType + "Action");
            moduleAction = (AtofModuleAction) cls.newInstance();
        } catch (InstantiationException ex) {
            throw new TestStepException(TestStepResultType.FAILURE,
                                        " InstantiationException when creation action type given " +
                                        actionType +
                                        "  for Communication module ..." +
                                        ex.getMessage());
        } catch (IllegalAccessException ex) {
            throw new TestStepException(TestStepResultType.FAILURE,
                                        " IllegalAccessException when creation action type given " +
                                        actionType +
                                        "  for Communication module ..." +
                                        ex.getMessage());
        } catch (ClassNotFoundException ex) {
            throw new TestStepException(TestStepResultType.FAILURE,
                                        " ClassNotFoundException when creation action type given " +
                                        actionType +
                                        "  for Communication module ..." +
                                        ex.getMessage());
        }

        return moduleAction;
    }
}
